package com.koron.order.experiment.plugin;

import com.alibaba.fastjson.JSONObject;
import com.koron.bean.base.Response;
import com.koron.bean.system.post.query.PostUserQuery;
import com.koron.bean.system.post.vo.PostUserVo;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.business.datadict.service.SysDataDictService;
import com.koron.common.core.business.system.feign.SystemFeignService;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.order.common.bean.vo.OrderDispatchVo;
import com.koron.order.experiment.controller.ExperimentController;
import com.koron.order.experiment.controller.ExperimentDispatchController;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName DefectWaitReviewPlugin
 * @description
 * @Author zhouj
 * @Date 2022/1/12 18:52
 */
@Slf4j
@RequestMapping("/experiment/plugin/waitReview/")
@RestController
@Api(tags = "缺陷工单-待审核插件")
public class ExperimentWaitReviewPlugin {

    @Autowired
    private WorkFlowHandler workFlowHandler;

    @Autowired
    private ExperimentController experimentController;

    @Autowired
    private ExperimentDispatchController experimentDispatchController;

    @Autowired
    private SysDataDictService sysDataDictService;

    @Autowired
    private SystemFeignService systemFeignService;

    //工作负责人确认 节点配置入口规则
    @PostMapping("/task1/submitter")
    public Response<List<WorkflowUser>> submitter1(@RequestBody SubmitterParam submitterParam) {
        /*
        节点1的过滤处理  工作负责人确认
        1、处理人	工作负责人（岗位）
        2、操作类型	驳回、沟通、转办
        3、必须变更审核人的节点	无
        4、自动过滤规则	只发送给当前试验工单的工作负责人
        5、通知方式	待办
        */
        String formVariables = submitterParam.getFormVariables();
        JSONObject jsonObject = JSONObject.parseObject(formVariables);
        String orderId = jsonObject.getString("id");

        List<WorkflowUser> result = new ArrayList<>();
        //先根据业务id查询出试验工单的工作负责人，直接返回
        Response<OrderDispatchVo> response = experimentDispatchController.queryDispatchInfoByOrderId(orderId);

        OrderDispatchVo data = response.getData();
        String leader =  data.getLeader();
        String leaderName =  data.getLeaderName();

        WorkflowUser workflowUser = new WorkflowUser();
        workflowUser.setCode(leader);
        workflowUser.setName(leaderName);
        result.add(workflowUser);

        return Response.ok(result);
    }

    //工作单位主任审核 节点配置入口规则
    @PostMapping("/task2/submitter")
    public Response<List<WorkflowUser>> submit2(@RequestBody SubmitterParam submitterParam) {

        /*
            1、处理人	电气室/主任（岗位）
            2、操作类型	驳回、沟通、转办
            3、必须变更审核人的节点	无
            4、通知方式	待办
        */
        //先分类
        List<WorkflowUser> workflowUsers = submitterParam.getUsers();
        List<WorkflowUser> gwList = workflowUsers.stream().filter(p -> 2 == p.getType()).collect(Collectors.toList());

        Assert.notEmpty(gwList, "配置的岗位不能为空");

        //分别通过各自id/code进行查询，或者过滤，最终转化为人员
        List<String> gwIdList = gwList.stream().map(WorkflowUser::getCode).collect(Collectors.toList());

        List<WorkflowUser> result = new ArrayList<>();
        //找到电气室的 主任(岗位)

        PostUserQuery postUserQuery = new PostUserQuery();

        //设置岗位ids
        postUserQuery.setPostIdList(gwIdList);
        //只拿 电气室(组织的主任)
        postUserQuery.setOrgNameList(Arrays.asList(new String[]{"电气室"}));

        //拿到岗位下的人
        List<PostUserVo> postUserVos = getPostList(postUserQuery);

        convertUser(postUserVos, result);

        return Response.ok(result);
    }

    //机电副总审批 节点配置入口规则
    @PostMapping("/task3/submitter")
    public Response<List<WorkflowUser>> submit3(@RequestBody SubmitterParam submitterParam) {

        /*
            1、处理人	部门副总经理（岗位）
            2、自动过滤规则	只发送给维修部的部门副总经理
            3、操作类型	驳回、沟通、转办
        */
        //先分类
        List<WorkflowUser> workflowUsers = submitterParam.getUsers();
        List<WorkflowUser> gwList = workflowUsers.stream().filter(p -> 2 == p.getType()).collect(Collectors.toList());

        Assert.notEmpty(gwList, "配置的岗位不能为空");

        //分别通过各自id/code进行查询，或者过滤，最终转化为人员
        List<String> gwIdList = gwList.stream().map(WorkflowUser::getCode).collect(Collectors.toList());

        List<WorkflowUser> result = new ArrayList<>();
        //找到维修部的 部门副总经理(岗位)

        PostUserQuery postUserQuery = new PostUserQuery();

        //设置岗位ids
        postUserQuery.setPostIdList(gwIdList);

        //只拿 部门副总经理(维修部下的副总)
        postUserQuery.setParentOrgNameList(Arrays.asList(new String[]{"维修部"}));

        //拿到岗位下的人
        List<PostUserVo> postUserVos = getPostList(postUserQuery);

        convertUser(postUserVos, result);

        return Response.ok(result);
    }

    private List<PostUserVo> getPostList(PostUserQuery postUserQuery) {
        Response<List<PostUserVo>> listResponse = systemFeignService.queryPostList(postUserQuery);
        List<PostUserVo> postUserVos = listResponse.getData();
        return postUserVos;
    }

    private void convertUser(List<PostUserVo> postUserVos, List<WorkflowUser> result) {
        //转换为返回对象
        if (CollectionUtils.isEmpty(postUserVos)) {
            return;
        }
        for (PostUserVo postUserVo : postUserVos) {
            WorkflowUser workflowUser = new WorkflowUser();
            workflowUser.setCode(postUserVo.getUserAccount());
            workflowUser.setName(postUserVo.getUserName());
            result.add(workflowUser);
        }
    }

}
